﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using HackApp.BusinessLogic;
using HackApp.Entities;
using HackApp.Models;

namespace HackApp.Controllers
{
  public class IdeaController : Controller
  {
    private IdeaLogic ideaLogic = BusinessFactory.GetBusinessObject(typeof(IdeaLogic)) as IdeaLogic;

    public ActionResult Index()
    {
      List<IdeaModel> model = new List<IdeaModel>();
      List<IdeaDTO> ideas = ideaLogic.GetIdeas();
      foreach (IdeaDTO idea in ideas)
      {
        IdeaModel ideaModel = new IdeaModel();
        ideaModel.ToVM(idea, true);
        ideaModel.VotesCount = ideaLogic.CountVotesForIdea(idea.Id);
        model.Add(ideaModel);
      }
      return View(model);
    }

    [Authorize]
    public ActionResult Add()
    {
      return View();
    }

    [HttpPost]
    [Authorize]
    public ActionResult Add(IdeaModel model)
    {
      if (ModelState.IsValid)
      {
        ideaLogic.AddIdea(model.ToDTO());
        return RedirectToAction("Index");
      }
      else return View(model);
    }

    [Authorize]
    public ActionResult Details(int? id)
    {
      if (id != null)
      {
        IdeaDTO ideaDTO = ideaLogic.GetById((int)id);
        if (ideaDTO != null)
        {
          IdeaModel model = new IdeaModel();
          model.ToVM(ideaDTO);

          ViewData.Add("VoteCount", ideaLogic.CountVotesForIdea((int)id));

          return View(model);
        }
        else return RedirectToAction("Index");
      }
      else return RedirectToAction("Index");
    }

    [Authorize]
    public ActionResult Vote(int? id)
    {
      if (id != null)
      {
        IdeaDTO ideaDTO = ideaLogic.GetById((int)id);
        if (ideaDTO != null)
        {
          IdeaModel model = new IdeaModel();
          model.ToVM(ideaDTO);
          return View(model);
        }
        else return RedirectToAction("Index");
      }
      else return RedirectToAction("Index");
    }

    public ActionResult VoteRequest(int? id)
    {
      if (id != null)
      {
        if (Request.IsAuthenticated)
        {
          bool voteRes = ideaLogic.Vote((int)id, Membership.GetUser());
          if (voteRes)
          {
            return Json(new JsonModel() { status = "success", message = "Voted!", data = ideaLogic.CountVotesForIdea((int)id) }, JsonRequestBehavior.AllowGet);
          }
          return Json(new JsonModel() { status = "error", message = "You already voted.", data = ideaLogic.CountVotesForIdea((int)id) }, JsonRequestBehavior.AllowGet);
        }
        return Json(new JsonModel() { status = "error", message = "You must be logged in in order to vote.", data = ideaLogic.CountVotesForIdea((int)id) }, JsonRequestBehavior.AllowGet);
      }
      return Json(new JsonModel() { status="error", message="You must specify an id.", data = ideaLogic.CountVotesForIdea((int)id) }, JsonRequestBehavior.AllowGet);
    }

    public ActionResult AddComment(int? id, PostModel model)
    {
      PostDTO dto = ideaLogic.AddComment((int)id, Membership.GetUser(), model.ToDTO());

      return Json(new JsonModel() { status = "success", message = "Added!", data = null }, JsonRequestBehavior.AllowGet);
    }

    public ActionResult GetComments(int? id)
    {
      if (id != null)
      {
        IEnumerable<PostDTO> list = ideaLogic.GetComments((int)id);
        List<PostModel> res = new List<PostModel>();
        for (int i = 0; i < list.Count(); i++)
        {
          PostModel model = new PostModel();
          model.ToVM(list.ElementAt(i));
          res.Add(model);
        }
        return Json(new JsonModel() { status = "success", message = "", data = res}, JsonRequestBehavior.AllowGet);
      }
      return Json(new JsonModel() { status = "error", message = "You must specify an id." }, JsonRequestBehavior.AllowGet);
    }
  }



  public class JsonModel
  {
    public string status { get; set; }
    public string message { get; set; }
    public object data { get; set; }
  }
}
